import React, { TextareaHTMLAttributes, useRef, useEffect } from 'react';

export interface TextAreaProps extends TextareaHTMLAttributes<HTMLTextAreaElement> {
  autoSize?: boolean | { minRows?: number; maxRows?: number };
  classNames?: Record<string, string>;
  styles?: Record<string, React.CSSProperties>;
}

const TextArea = React.forwardRef<HTMLTextAreaElement, TextAreaProps>((props, ref) => {
  const { autoSize, classNames, styles, ...rest } = props;
  const localRef = useRef<HTMLTextAreaElement>(null);
  const innerRef = (ref as React.RefObject<HTMLTextAreaElement>) || localRef;

  useEffect(() => {
    if (autoSize && innerRef.current) {
      const textarea = innerRef.current;
      textarea.style.height = 'auto';
      textarea.style.height = `${textarea.scrollHeight}px`;
    }
  }, [props.value, autoSize, innerRef]);

  return (
    <textarea
      ref={innerRef}
      className={`input-textarea ${classNames?.textarea || ''}`}
      style={styles?.textarea}
      {...rest}
    />
  );
});

export default TextArea; 